package com.tx.dao.impl;

import com.tx.dao.UserDao;
import com.tx.entity.User;
import com.tx.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public User findByAccountAndPassword(String account, String password) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "SELECT * FROM user WHERE account = ? AND password = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, account);
            ps.setString(2, password);
            rs = ps.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setAccount(rs.getString("account"));
                user.setPassword(rs.getString("password"));
                user.setPermission(rs.getInt("permission"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, ps, conn);
        }
        return user;
    }

    @Override
    public int add(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "INSERT INTO user (username, account, password, permission) VALUES (?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getAccount());
            ps.setString(3, user.getPassword());
            ps.setInt(4, user.getPermission());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, ps, conn);
        }
        return 0;
    }

    @Override
    public int update(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtils.getConnection();
            StringBuilder sql = new StringBuilder("UPDATE user SET username = ?, permission = ?");
            List<Object> params = new ArrayList<>();
            params.add(user.getUsername());
            params.add(user.getPermission());

            if (user.getPassword() != null && !user.getPassword().isEmpty()) {
                sql.append(", password = ?");
                params.add(user.getPassword());
            }
            if (user.getAccount() != null && !user.getAccount().isEmpty()) {
                sql.append(", account = ?");
                params.add(user.getAccount());
            }
            sql.append(" WHERE id = ?");
            params.add(user.getId());

            ps = conn.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                ps.setObject(i + 1, params.get(i));
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, ps, conn);
        }
        return 0;
    }

    @Override
    public int delete(int id) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "DELETE FROM user WHERE id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, ps, conn);
        }
        return 0;
    }

    @Override
    public List<User> findAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<>();
        try {
            conn = JDBCUtils.getConnection();
            String sql = "SELECT * FROM user";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setAccount(rs.getString("account"));
                user.setPassword(rs.getString("password"));
                user.setPermission(rs.getInt("permission"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, ps, conn);
        }
        return users;
    }

    @Override
    public List<User> findByUsername(String username) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<>();
        try {
            conn = JDBCUtils.getConnection();
            String sql = "SELECT * FROM user WHERE username LIKE ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, "%" + username + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setAccount(rs.getString("account"));
                user.setPassword(rs.getString("password"));
                user.setPermission(rs.getInt("permission"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, ps, conn);
        }
        return users;
    }
}